using System;
using System.Collections.Generic;
using System.Linq;
using Headsight.BPMN2WF.BPMN.Schemas;

namespace Headsight.BPMN2WF.BPMN.Model
{
    /// <summary>
    /// <i>BPMN2WF</i> <see cref="tFlowElement"/> representation.
    /// </summary>
    public class BPMNFlowNode : BPMNBaseElement<tFlowNode>
    {
        /// <summary>
        /// Constructs a new instance for the <see cref="tFlowElement"/> <para>tFlowElement</para>.
        /// </summary>
        public BPMNFlowNode(tFlowNode tFlowNode, IList<BPMNArgument> argumentsIn, IList<BPMNArgument> argumentsOut) : base(tFlowNode, tFlowNode.id)
        {
            ArgumentsIn = argumentsIn;
            ArgumentsOut = argumentsOut;
        }

        /// <summary>
        /// The <see cref="BPMNFlowNode"/>'s <i>in-arguments</i>.
        /// </summary>
        public IList<BPMNArgument> ArgumentsIn { get; private set; }

        /// <summary>
        /// The <see cref="BPMNFlowNode"/>'s <i>out-arguments</i>.
        /// </summary>
        public IList<BPMNArgument> ArgumentsOut { get; private set; }

        /// <summary>
        /// Returns the <see cref="BPMNFlowNode"/>'s <i>in-arguments</i>.
        /// </summary>
        public IList<BPMNArgument> GetArgumentsIn()
        {
            var result = new List<BPMNArgument>();

            var tActivity = TElement as tActivity;

            if (tActivity == null)
            {
                return result;
            }

            var properties = tActivity.ioSpecification.dataInput;

            foreach (var each in properties)
            {
                var dataInputAssoc = tActivity.dataInputAssociation.First(x => x.targetRef == each.id);

                var assignemtn = dataInputAssoc.assignment[0];
                var from = assignemtn.from;
                var fromText = from.Text;

                var exp = (fromText != null && fromText.Length >= 1) ? fromText[0] : String.Empty;

                result.Add(new BPMNArgument(each.name, typeof(String), exp));
            }

            return result;
        }

        public override string ToString()
        {
            return String.Format("{0}[Id: {1}, Name: {2}]", GetType().Name, Id, TElement.name);
        }
    }
}